package com.clubs.coding.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.clubs.coding.Service.MakeTimeService;
import com.clubs.coding.common.ApiRestResponse;
import com.clubs.coding.exception.ImoocMallException;
import com.clubs.coding.exception.ImoocMallExceptionEnum;
import com.clubs.coding.model.Vo.MakeItemNoVo;
import com.clubs.coding.model.dao.ApplicationMapper;
import com.clubs.coding.model.dao.MakeTimeMapper;
import com.clubs.coding.model.pojo.MakeTime;
import com.clubs.coding.model.request.DataReq;
import com.clubs.coding.model.request.MakeTimeReq;
import com.clubs.coding.model.request.MakeTimeSelectReq;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/admin/make")

public class MakeAdminTimeController {
    @Autowired
    MakeTimeService makeTimeService;
    @Autowired
    MakeTimeMapper makeTimeMapper;
    @Autowired
    ApplicationMapper applicationMapper;
    @PostMapping("/upload")
    public ApiRestResponse upload(@RequestBody @Valid MakeTimeReq makeTimeReq){
        makeTimeService.upload(makeTimeReq);
        return ApiRestResponse.success("上传成功");
    }
    /**
    * 课程列表
    * */
    @PostMapping("/list")
    public ApiRestResponse list(@RequestBody @Valid MakeTimeSelectReq makeTimeSelectReq){
        if (makeTimeSelectReq.getType()==0) {

            PageInfo list = makeTimeService.list(makeTimeSelectReq);
            return ApiRestResponse.success(list);
        }else if(makeTimeSelectReq.getType()==1){
            //查询开课列表满足3人报名的课程
            PageHelper.startPage(makeTimeSelectReq.getPageNum(), makeTimeSelectReq.getPageSize());
            List<MakeItemNoVo> makeItemNoVos = applicationMapper.selectByresidue(makeTimeSelectReq);
            PageInfo pageInfo = new PageInfo(makeItemNoVos);
            return ApiRestResponse.success(pageInfo);
        }else{
            MakeTime makeTime = new MakeTime();
            BeanUtils.copyProperties(makeTimeSelectReq,makeTime);
            makeTime.setMakeTimeId(makeTimeSelectReq.getId());
            System.out.println(makeTime);
            int i = makeTimeMapper.updateByPrimaryKeySelective(makeTime);
            if(i<0){
                new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
            }
            return ApiRestResponse.success();
        }


    }

    /*
   上传接口
    */
//MultipartFile用于接受前端传过来的file对象
    @PostMapping("/file/upload")
    public ApiRestResponse fileupload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();//获取文件的名称

    /*
    我们需要给我们的文件加一个文件前缀，不可以加文件后缀，因为我们的文件还有后缀名
    加文件前缀的目的是为了防止文件重名，文件重名的话后续的重名文件会覆盖掉前面的文件
     */
        String flag = IdUtil.fastSimpleUUID();//通过Hutool工具包的IdUtil类获取uuid作为前缀
        String rootFilePath = System.getProperty("user.dir") + "/src/main/resources/files/" + flag + "_" + filename;//文件路径
        FileUtil.writeBytes(file.getBytes(), rootFilePath);//使用Hutool工具包将我们接收到文件保存到rootFilePath中


        File files = FileUtil.file(rootFilePath);   //导入文件
        String type = FileTypeUtil.getType(files);   //判断文件类型
        if (!type.equals("xlsx")) {
            FileUtil.del(files);//删除文件
            throw new ImoocMallException(ImoocMallExceptionEnum.FILE_UP_NULL);
        }
        ExcelReader reader = ExcelUtil.getReader(rootFilePath);//读取excel文件
        List<MakeTimeReq> readAll = reader.readAll(MakeTimeReq.class);//转化为maketimereq List对象
        for (int i = 0; i < readAll.size(); i++) {
            MakeTimeReq makeTimeReq =  readAll.get(i);
            Date date = makeTimeReq.getThatVeryDay();
            if (date.before(new Date())) {//判断上传的时间是不是都在当前时间之后
                FileUtil.del(files);//删除文件

                throw new ImoocMallException(ImoocMallExceptionEnum.DATA_before);
            }

        }
        int i = makeTimeMapper.insertBatch(readAll);
        return ApiRestResponse.success(i);
    }

//    @PostMapping("/residue")
//    public ApiRestResponse residue( @RequestBody @Valid DataReq dataReq){
//        List<MakeItemNoVo> makeItemNoVos = applicationMapper.selectByresidue(dataReq);
//        return ApiRestResponse.success(makeItemNoVos);
//    }

}
